<?php
//default sorting
if(!$_SESSION['article_sort']){
	$e=explode("|", $conf['article_sort']);
	$_SESSION['article_sortby']=string_template($e[0], array('lang'=>$_SESSION['lang_short']));
	$_SESSION['article_sort']=$e[1];
}
//

//prints list of articles (titles in session[language]) - w/o subcategories
function articles($category, $chars, $template, $by='date', $order='DESC') {
	global $conf, $lang;

	SQLvalidate($category);
	$db = new dbquery;
	$db->query("SELECT * FROM $conf[prefix]article WHERE cat=$category and visible=1 and text_$_SESSION[lang_short] != '' ORDER by $by $order") or $db->err(__FILE__, __LINE__);

	$list = null;

	while($art = $db->fetch_object()) {
		$tl='title_'.$_SESSION['lang_short'];
		$t='text_'.$_SESSION['lang_short'];

		if($art->$tl==''){
			$tl='title_'.$conf['language'];
			$t='text_'.$conf['language'];
		}

		$title = read_text_rest($art->$tl);
		$text = strip_exbcode(add_dots_($art->$t, 200));

		if($conf['mod_rewrite']) //jezeli wlaczony mod_rewrite
		$url='article-'.read_text_mr($art->$tl).'_'.$art->id.'_0.html';
		else
		$url='index.php?module=article&amp;id='.$art->id;

		$overLIB=string_template($lang['article_overlib'], array('theme_path'=>$GLOBALS['theme_path'], 'title'=>$title, 'text'=>$text, 'rating'=>$art->rating, 'votes'=>$art->votes));
		$overLIB=overlib_text($overLIB);
		$overLIB=" onmouseover=\"return overlib('$overLIB');\" onmouseout=\"return nd();\" ";

		$title=substr($title, 0,$chars);
		$list .= string_template($template, array('url' => $url, 'id' => $art->id, 'name' => $title, 'date' => date($conf['date_format'], strtotime($art->date)), 'author' => get_user_link_by_id($art->author), 'read' => $art->viewed, 'overlib' => $overLIB));
	}
	echo($list);
}

//prints list of new articles
function articles_new($limit, $chars, $template, $subcat = 0, $cat = 0, $order='DESC', $orderby='date') {
	global $conf, $lang;

	SQLvalidate($subcat);

	$zapytanie = "SELECT * FROM $conf[prefix]article ";
	if($subcat) {
		$cat_[]=$subcat;
		$cat_=array_merge($cat_, get_subcategories($subcat));
		$zapytanie .=" WHERE visible=1 && cat IN (".implode(', ', $cat_).") && text_$_SESSION[lang_short] != ''";
	}
	elseif($cat){
		$zapytanie .=" WHERE cat IN (".$cat.") &&  visible=1 && text_$_SESSION[lang_short] != ''";
	}
	else
	$zapytanie .=" WHERE visible=1 && text_$_SESSION[lang_short] != ''";

	$zapytanie.="ORDER BY $orderby $order LIMIT 0,$limit";

	$db = new dbquery;
	$db->query($zapytanie) or $db->err(__FILE__, __LINE__);
	$list = null;
	while($art = $db->fetch_object()) {
		$tl='title_'.$_SESSION['lang_short'];
		$t='text_'.$_SESSION['lang_short'];

		if($art->$tl==''){
			$tl='title_'.$conf['language'];
			$t='text_'.$conf['language'];
		}

		$title = read_text_rest($art->$tl);
		$text = strip_exbcode(add_dots_($art->$t, 200));

		if($conf['mod_rewrite']) //jezeli wlaczony mod_rewrite
		$url='article-'.read_text_mr($art->$tl).'_'.$art->id.'_0.html';
		else
		$url='index.php?module=article&amp;id='.$art->id;

		$overLIB=string_template($lang['article_overlib'], array('theme_path'=>$GLOBALS['theme_path'], 'title'=>$title, 'text'=>$text, 'rating'=>$art->rating, 'votes'=>$art->votes));
		$overLIB=overlib_text($overLIB);
		$overLIB=" onmouseover=\"return overlib('$overLIB');\" onmouseout=\"return nd();\" ";

		$title=substr($title, 0,$chars);
		$list .= string_template($template, array('url' => $url, 'id' => $art->id, 'name' => $title, 'date' => date($conf['date_format'], strtotime($art->date)), 'author' => get_user_link_by_id($art->author), 'read' => $art->viewed, 'overlib' => $overLIB));
	}
	echo($list);
}

//prints list of highest rated articles
function articles_highest_rated($limit, $chars, $template, $subcat = 0, $cat = 0, $order='DESC', $orderby='rating') {
	global $conf, $lang;

	SQLvalidate($subcat);

	$zapytanie = "SELECT * FROM $conf[prefix]article ";
	if($subcat) {
		$cat_[]=$subcat;
		$cat_=array_merge($cat_, get_subcategories($subcat));
		$zapytanie .=" WHERE cat IN (".implode(', ', $cat_).") && visible=1 && text_$_SESSION[lang_short] != ''";
	}
	elseif($cat){
		$zapytanie .=" WHERE cat IN (".$cat.") &&  visible=1 && text_$_SESSION[lang_short] != ''";
	}
	else
	$zapytanie .=" WHERE visible=1 && text_$_SESSION[lang_short] != ''";

	$zapytanie.="ORDER BY $orderby $order LIMIT 0,$limit";

	$db = new dbquery;
	$db->query($zapytanie) or $db->err(__FILE__, __LINE__);
	$list = null;

	while($art = $db->fetch_object()) {
		$tl='title_'.$_SESSION['lang_short'];
		$t='text_'.$_SESSION['lang_short'];

		if($art->$tl==''){
			$tl='title_'.$conf['language'];
			$t='text_'.$conf['language'];
		}

		$title = read_text_rest($art->$tl);
		$text = strip_exbcode(add_dots_($art->$t, 200));

		if($conf['mod_rewrite']) //jezeli wlaczony mod_rewrite
		$url='article-'.read_text_mr($art->$tl).'_'.$art->id.'_0.html';
		else
		$url='index.php?module=article&amp;id='.$art->id;

		$overLIB=string_template($lang['article_overlib'], array('theme_path'=>$GLOBALS['theme_path'], 'title'=>$title, 'text'=>$text, 'rating'=>$art->rating, 'votes'=>$art->votes));
		$overLIB=overlib_text($overLIB);
		$overLIB=" onmouseover=\"return overlib('$overLIB');\" onmouseout=\"return nd();\" ";

		$title=substr($title, 0,$chars);
		$list .= string_template($template, array('url' => $url, 'id' => $art->id, 'name' => $title, 'date' => date($conf['date_format'], strtotime($art->date)), 'author' => get_user_link_by_id($art->author), 'read' => $art->viewed, 'overlib' => $overLIB));
	}
	echo($list);
}

//prints list of most popular articles
function articles_most_popular($limit, $chars, $template, $subcat = 0, $cat = 0, $order='DESC', $orderby='viewed') {
	global $conf, $lang;

	$zapytanie = "SELECT * FROM $conf[prefix]article ";
	if($subcat) {
		$cat_[]=$subcat;
		$cat_=array_merge($cat_, get_subcategories($subcat));
		$zapytanie .=" WHERE cat IN (".implode(', ', $cat_).") && visible=1 && text_$_SESSION[lang_short] != ''";
	}
	elseif($cat){
		$zapytanie .=" WHERE cat IN (".$cat.") &&  visible=1 && text_$_SESSION[lang_short] != ''";
	}
	else
	$zapytanie .=" WHERE visible=1 && text_$_SESSION[lang_short] != ''";

	$zapytanie.="ORDER BY $orderby $order LIMIT 0,$limit";

	$db = new dbquery;
	$db->query($zapytanie) or $db->err(__FILE__, __LINE__);
	$list = null;

	while($art = $db->fetch_object()) {
		$tl='title_'.$_SESSION['lang_short'];
		$t='text_'.$_SESSION['lang_short'];

		if($art->$tl==''){
			$tl='title_'.$conf['language'];
			$t='text_'.$conf['language'];
		}

		$title = read_text_rest($art->$tl);
		$text = strip_exbcode(add_dots_($art->$t, 200));

		if($conf['mod_rewrite']) //jezeli wlaczony mod_rewrite
		$url='article-'.read_text_mr($art->$tl).'_'.$art->id.'_0.html';
		else
		$url='index.php?module=article&amp;id='.$art->id;

		$overLIB=string_template($lang['article_overlib'], array('theme_path'=>$GLOBALS['theme_path'], 'title'=>$title, 'text'=>$text, 'rating'=>$art->rating, 'votes'=>$art->votes));
		$overLIB=overlib_text($overLIB);
		$overLIB=" onmouseover=\"return overlib('$overLIB');\" onmouseout=\"return nd();\" ";

		$title=substr($title, 0, $chars);
		$list .= string_template($template, array('url' => $url, 'id' => $art->id, 'name' => $title, 'date' => date($conf['date_format'], strtotime($art->date)), 'author' => get_user_link_by_id($art->author), 'read' => $art->viewed, 'overlib' => $overLIB));
	}
	echo($list);
}
?>
